﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Excel;
using Microsoft.Office.Tools.Ribbon;
using RedmineToolBar.Model;
using RedmineToolBar.Redmine.Configuration;
using RedmineToolBar.Redmine.Downloader;
using RedmineToolBar.Utility;
namespace RedmineToolBar
{
    public partial class FrmImpostazioni : Form
    {

        public FrmImpostazioni()
        {
            InitializeComponent();
        }

        private void FrmImpostazioni_Load(object sender, EventArgs e)
        {


            RedmineConfigurationBuilder rb = new RedmineConfigurationBuilder();
            if (!rb.isWorkbookConnected())
            {
                RibbonUtility.SetEnable(RibbonStatus.NOT_CONFIGURED);

            }
            else if (!rb.isQuerySelected())
            {
                RibbonUtility.SetEnable(RibbonStatus.CONFIGURED);

            }
            else
            {
                RibbonUtility.SetEnable(RibbonStatus.CONNECTED_TO_QUERY);
            }
            var conf = rb.GetRedmineCongfiguration();
            var rd = rb.GetDownloader();
            List<RedmineQuery> res = rd.GetRedmineQuery();
            listQuery.DataSource = res;
            listQuery.DisplayMember = "name";

        }

        private void btnCollega_Click(object sender, EventArgs e)
        {

            Worksheet worksheetNew = null;

            RedmineConfigurationBuilder rb = new RedmineConfigurationBuilder();

            var conf = rb.GetRedmineCongfiguration();


            if (conf.QueryId != null && conf.QueryId != ((RedmineQuery)listQuery.SelectedItem).id.ToString())
            {
                var esito = MessageBox.Show("Attenzione il foglio precedentemente collegato verrà scollegato.", "Segnalazione", MessageBoxButtons.OKCancel);

                if (DialogResult.Cancel.Equals(esito)) return;
            }

            rb.SetQuery(((RedmineQuery)listQuery.SelectedItem).id.ToString(), ((RedmineQuery)listQuery.SelectedItem).name, ((RedmineQuery)listQuery.SelectedItem).project_id)
                .SetExcludeDescription(chkExludeDescription.Checked)
                .SetLastUpdate(DateTime.Now)
                .Save();


            try
            {
                worksheetNew = Globals.Factory.GetVstoObject(Globals.ThisAddIn.Application.ActiveWorkbook.Worksheets[conf.QueryName]);
            }
            catch (Exception ex1)
            {

                Console.WriteLine(ex1.StackTrace);
            }



            if (worksheetNew == null)
            {
                Globals.ThisAddIn.Application.ActiveWorkbook.Worksheets.Add();

                worksheetNew = Globals.Factory.GetVstoObject(Globals.ThisAddIn.Application.ActiveWorkbook.ActiveSheet);
                worksheetNew.Name = conf.QueryName;

            }

            try
            {
                var rd = rb.GetDownloader();
                rd.UpdateSheet(worksheetNew);
             

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            this.Hide();

            RibbonUtility.SetEnable(RibbonStatus.CONNECTED_TO_QUERY);


        }


    }
}
